package com.wyzz.hr.action;

import java.io.PrintStream;
import java.math.BigDecimal;
import java.util.*;
import org.apache.axis.utils.StringUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.util.ObjectUtils;
import org.testng.log4testng.Logger;

import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IEmpSalaryAcctitemsBo;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.ISalaryPaidBo;
import com.wyzz.hr.bo.ISalaryconfBo;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalaryacctitems;
import com.wyzz.hr.pojo.Empsalaryconfig;
import com.wyzz.hr.pojo.Empsalarypay;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.StringUtil;

public class UpdateSalaryConfigBatch extends CompAction {

	public UpdateSalaryConfigBatch() {
	}

	public String updateConfigBatch(String empIds, String itemId,
			BigDecimal salaryValue) {
		if ("error".equalsIgnoreCase(DWRUtil.checkAuth("updateSalaryConfig",
				"updateSalaryConfig")))
			return StringUtil.message(msgNoAuth, new Object[] { "noauth" });
		if (StringUtils.isEmpty(empIds) || StringUtils.isEmpty(itemId)
				|| salaryValue == null)
			return StringUtil.message(msgNoParam, new Object[] { "noparam" });
		List configList = updateConfigBatch_DC(empIds.split(","));
		if (configList == null || configList.size() == 0)
			return StringUtil.message(msgNoData, new Object[] { "noconf",
					"\u53EF\u4EE5\u4FEE\u6539\u7684\u85AA\u8D44\u914D\u7F6E" });
		IEmpSalaryAcctitemsBo esaiBo = (IEmpSalaryAcctitemsBo) SpringBeanFactory
				.getBean("empsalaryacctitemsBo");
		if (!esaiBo.setAcctItemsConfig((Empsalaryconfig[]) configList
				.toArray(new Empsalaryconfig[configList.size()])))
			return StringUtil.message(msgNoData, new Object[] { "noacct",
					"\u5E10\u5957\u6570\u636E" });
		setConfigValues(itemId, salaryValue, configList);
		if (configList == null || configList.size() == 0)
			return StringUtil.message(msgNoData, new Object[] { "noconf",
					"\u53EF\u4EE5\u4FEE\u6539\u7684\u85AA\u8D44\u914D\u7F6E" });
		ISalaryconfBo confBo = (ISalaryconfBo) getBean("salaryconfBo");
		confBo.interpretConfig((Empsalaryconfig[]) configList
				.toArray(new Empsalaryconfig[configList.size()]));
		if (configList == null || configList.size() == 0) {
			return StringUtil.message(msgNoData, new Object[] { "noconf",
					"\u53EF\u4EE5\u4FEE\u6539\u7684\u85AA\u8D44\u914D\u7F6E" });
		} else {
			String str = confBo
					.batchUpdateConfig(getCurrentEmpNo(), configList);
			return StringUtil.message(msgAdjConfSucc, new Object[] { "SUCC",
					str });
		}
	}

	public String updatePayBatch(String empIds, String itemId,
			String salaryValue, String yearmonth) {
		if ("error".equalsIgnoreCase(DWRUtil.checkAuth("searchSalaryPaid",
				"execute")))
			return StringUtil.message(msgNoAuth, new Object[] { "noauth" });
		if (StringUtils.isEmpty(salaryValue) || StringUtils.isEmpty(itemId))
			return StringUtil.message(msgNoParam, new Object[] { "noparam" });
		if (StringUtils.isEmpty(yearmonth))
			return StringUtil.message(msgNoYm, new Object[] { "noym" });
		if (!checkSalaryPeriod(yearmonth, new Integer[] { Integer.valueOf(0) }))
			return StringUtil.message(msgPeriodInitErr, new Object[0]);
		if (StringUtils.isEmpty(empIds))
			return StringUtil.message(msgNoEmp, new Object[] { "noemp" });
		String empIdArr[] = empIds.split(",");
		if (ObjectUtils.isEmpty(empIdArr))
			return StringUtil.message(msgNoEmp, new Object[] { "noemp" });
		BigDecimal bdValue;
		try {
			bdValue = new BigDecimal(salaryValue);
		} catch (Exception e) {
			return StringUtil.message(msgFormatErr, new Object[] { "format",
					"\u9879\u76EE\u6570\u503C" });
		}
		List payList = updatePayBatch_DC(yearmonth, empIdArr);
		if (payList == null || payList.size() == 0)
			return StringUtil.message(msgNoData, new Object[] { "nopay",
					"\u53EF\u4EE5\u4FEE\u6539\u7684\u85AA\u8D44\u53D1\u653E" });
		setPayAdd(yearmonth, (Empsalarypay[]) payList
				.toArray(new Empsalarypay[payList.size()]));
		setPayValues(itemId, bdValue, payList);
		interpretPay(yearmonth, true, (Empsalarypay[]) payList
				.toArray(new Empsalarypay[payList.size()]));
		if (payList == null || payList.size() == 0) {
			return StringUtil.message(msgNoData, new Object[] { "nopay",
					"\u53EF\u4EE5\u4FEE\u6539\u7684\u85AA\u8D44\u53D1\u653E" });
		} else {
			ISalaryPaidBo salaryPaidBo = (ISalaryPaidBo) getBean("salaryPaidBo");
			String str = salaryPaidBo.batchUpdatePay(yearmonth, payList);
			return StringUtil.message(msgAdjPaySucc,
					new Object[] { "SUCC", str });
		}
	}

	public List updateConfigBatch_DC(String empIdArr[]) {
		org.hibernate.criterion.DetachedCriteria dc = updateConfigBatch_DC();
		if ("SUB".equals(super.authorityCondition))
			addSubDC(dc, "emp", 2);
		BaseCrit.addDC(dc, Employee.PROP_ID, "in", empIdArr);
		BaseCrit.addDC(dc, Employee.PROP_EMP_STATUS, "eq",
				new Integer[] { Integer.valueOf(1) });
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		List empList = empBo.findByCriteria(dc);
		if (empList == null || empList.size() == 0)
			return null;
		List configList = new ArrayList();
		Empsalaryconfig config;
		for (Iterator i$ = empList.iterator(); i$.hasNext(); configList
				.add(config)) {
			Employee emp = (Employee) i$.next();
			config = emp.getConfig();
			config.decryEmpSalaryConf(config);
		}

		return configList;
	}

	public List updatePayBatch_DC(String yearmonth, String empIdArr[]) {
		org.hibernate.criterion.DetachedCriteria dc = updatePayBatch_DC();
		if ("SUB".equals(super.authorityCondition))
			addSubDC(dc, "emp", 2);
		BaseCrit.addDC(dc, Empsalarypay.PROP_ESP_YEARMONTH, "eq",
				new String[] { yearmonth });
		BaseCrit.addDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_ID).toString(), "in", empIdArr);
		BaseCrit.addStatusEmpDC(dc, "emp", yearmonth, new String[] { "" });
		ISalaryPaidBo salaryPaidBo = (ISalaryPaidBo) getBean("salaryPaidBo");
		List payList = salaryPaidBo.findPay(dc, null);
		if (payList.size() == 0)
			return null;
		Empsalaryconfig config;
		for (Iterator i$ = payList.iterator(); i$.hasNext(); config
				.decryEmpSalaryConf(config)) {
			Empsalarypay pay = (Empsalarypay) i$.next();
			config = pay.getEspEmpconfig();
			pay.decryEMPPaid(pay);
		}

		return payList;
	}

	private boolean setConfigValues(String itemId, BigDecimal bdValue,
			List configList) {
		if ((configList == null) || (configList.size() == 0))
			return false;
		for (Iterator it = configList.iterator(); it.hasNext();) {
			Empsalaryconfig config = (Empsalaryconfig) it.next();
			List acctItems = config.getAcctItems();
			boolean result = false;
			for (int i = 0; i < acctItems.size(); ++i) {
				Empsalaryacctitems item = (Empsalaryacctitems) acctItems.get(i);
				try {
					if (item.getEsaiDataIsCalc().intValue() == 0) {
						if (item.getId().equals(itemId)) {
							PropertyUtils.setProperty(config, "escColumn"
									+ (i + 1), bdValue.setScale(2));
							result = true;
						}
					}
				} catch (Exception e) {
					System.out.println(e);
					result = false;
				}
			}
			if (!result)
				it.remove();
		}
		return configList.size() != 0;
	}

	private boolean setPayValues(String itemId, BigDecimal bdValue, List payList) {
		if ((payList == null) || (payList.size() == 0))
			return false;
		for (Iterator it = payList.iterator(); it.hasNext();) {
			Empsalarypay pay = (Empsalarypay) it.next();
			Empsalaryconfig config = pay.getEspEmpconfig();
			List acctItems = pay.getAcctItems();
			pay.setEspChanged(Integer.valueOf(0));
			boolean result = false;
			for (int i = 0; i < acctItems.size(); ++i) {
				Empsalaryacctitems item = (Empsalaryacctitems) acctItems.get(i);
				try {
					if (item.getEsaiDataIsCalc().intValue() != 2) {
						if (item.getId().equals(itemId)) {
							PropertyUtils.setProperty(pay, "espColumn"
									+ (i + 1), bdValue);
							result = true;
						}
						Object escValue = PropertyUtils.getProperty(config,
								"escColumn" + (i + 1));
						Object espValue = PropertyUtils.getProperty(pay,
								"espColumn" + (i + 1));
						if ((escValue != null) || (espValue != null)) {
							if ((escValue == null)
									|| (((BigDecimal) escValue)
											.compareTo((BigDecimal) espValue) != 0))
								pay.setEspChanged(Integer.valueOf(1));
						}
					}
				} catch (Exception e) {
					System.out.println(e);
					result = false;
				}
			}
			if (!result)
				it.remove();
		}
		return payList.size() != 0;
	}

	private static Logger logger = Logger
			.getLogger(UpdateSalaryConfigBatch.class);
	private static final long serialVersionUID = 1L;

}
